<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="客户号" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户姓名" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户领取时间" prop="getTime">
        <el-date-picker clearable
                        v-model="queryParams.getTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择客户领取时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="礼品发放人员" prop="grantStaff">
        <el-input
          v-model="queryParams.grantStaff"
          placeholder="请输入礼品发放人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注信息" prop="grantRemark">
        <el-input
          v-model="queryParams.grantRemark"
          placeholder="请输入备注信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin-left: 30px">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kycinfo:grant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kycinfo:grant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kycinfo:grant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kycinfo:grant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 表格列筛选 -->
    <div style="float: right;margin: 10px;">
      <el-popover placement="right" width="" trigger="click"><!--弹出框-->
        <el-checkbox-group v-model="tableFilterData.checkBoxList">
          <template v-for="(item, i) in tableFilterData.dataNow2">
            <el-checkbox :label="item.attr" :key="i" checked style="display: block;margin:10px;" @change="filterFunHandle('filter',item.attr,i)">
              {{item.label}}
            </el-checkbox>
          </template>
        </el-checkbox-group>
        <el-button  slot="reference"><i class="el-icon-arrow-down el-icon-menu" /> 列筛选</el-button>
        <el-button  type="text" @click="filterFunHandle('allchecked')">全选</el-button>
        <el-button  type="text" @click="filterFunHandle('cancel')">取消全选</el-button>
      </el-popover>
    </div>

    <!-- 表格 -->
    <el-table  border  :data="grantList" style="margin: 10px; "@selection-change="handleSelectionChange">
      <el-table-column type="selection" width="30" align="center" />
      <el-table-column type="index" label="序号" align="center"/>
      <template v-for="(col,index) in tableFilterData.dataNow">
        <el-table-column :key="index" align="center" :prop="col.attr" :label="col.label" min-width="100" sortable>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kycinfo:grant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kycinfo:grant:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改营销宣传品发放记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户号" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入客户号" />
        </el-form-item>
        <el-form-item label="客户姓名" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item prop="clientLevel" label="客户层级">
          <el-select v-model="form.clientLevel">
            <el-option v-for="item in options"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
<!--            <el-option :value="大众客户" label="大众客户"></el-option>-->
<!--            <el-option :value="富裕客户" label="富裕客户"></el-option>-->
<!--            <el-option :value="浩瀚金" label="浩瀚金"></el-option>-->
<!--            <el-option :value="浩瀚白金" label="浩瀚白金"></el-option>-->
<!--            <el-option :value="浩瀚钻石" label="浩瀚钻石"></el-option>-->
<!--            <el-option :value="私人银行" label="私人银行"></el-option>-->
          </el-select>
        </el-form-item>
        <el-form-item label="客户领取时间" prop="getTime">
          <el-date-picker clearable
                          v-model="form.getTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择客户领取时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="礼品发放人员" prop="grantStaff">
          <el-input v-model="form.grantStaff" placeholder="请输入礼品发放人员" />
        </el-form-item>
        <el-form-item prop="getType" label="领取礼品品类">
          <el-select v-model="form.getType">
            <el-option v-for="item in options1"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
<!--            <el-option :value="优惠券类" label="优惠券类"></el-option>-->
<!--            <el-option :value="粮油类" label="粮油类"></el-option>-->
<!--            <el-option :value="折扣类" label="折扣类"></el-option>-->
<!--            <el-option :value="其他" label="其他"></el-option>-->
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息" prop="grantRemark">
          <el-input v-model="form.grantRemark" placeholder="请输入备注信息" />
        </el-form-item>
        <!--        <el-form-item label="备注" prop="remark">-->
        <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGrant, getGrant, delGrant, addGrant, updateGrant } from "@/api/kycinfo/grant";



export default {
  name: "Grant",
  data() {
    return {
      options: [{
        value: '大众客户',
        label: '大众客户'
      }, {
        value: '富裕客户',
        label: '富裕客户'
      }, {
        value: '浩瀚金',
        label: '浩瀚金'
      }, {
        value: '浩瀚白金',
        label: '浩瀚白金'
      }, {
        value: '浩瀚钻石',
        label: '浩瀚钻石'
      }, {
        value: '私人银行',
        label: '私人银行'
      }],
      options1: [{
        value: '优惠券类',
        label: '优惠券类'
      }, {
        value: '粮油类',
        label: '粮油类'
      }, {
        value: '折扣类',
        label: '折扣类'
      }, {
        value: '其他',
        label: '其他'
      },],
      getType:'',
      //表格列筛选
      tableFilterData:{
        dataNow2: [//保存默认的数据
          {label: "序号", attr: "id"},
          {label: "客户号", attr: "clientId"},
          {label: "客户姓名", attr: "clientName"},
          {label: "客户层级", attr: "clientLevel",},
          {label: "领取礼品品类", attr: "getType2"},
          {label: "客户领取时间", attr: "getTime"},
          {label: "礼品发放人员", attr: "grantStaff"},
          {label: "备注信息", attr: "grantRemark"},
          // {label: "备注", attr: "remark"}
        ],
        dataNow: [//操作时候动态变化的数据
          // {label: "序号", attr: "id"},
          {label: "客户号", attr: "clientId"},
          {label: "客户姓名", attr: "clientName"},
          {label: "客户层级", attr: "clientLevel"},
          {label: "领取礼品品类", attr: "getType"},
          {label: "客户领取时间", attr: "getTime"},
          {label: "礼品发放人员", attr: "grantStaff"},
          {label: "备注信息", attr: "grantRemark"},
          // {label: "备注", attr: "remark"}
        ],
        checkLabels: [], // 筛选列显示多选框数据
        checkBoxList: [] // 筛选列数据
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营销宣传品发放记录表格数据
      grantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientId: null,
        clientName: null,
        clientLevel: null,
        getType: null,
        getTime: null,
        grantStaff: null,
        grantRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询营销宣传品发放记录
     列表 */// 列筛选
    filterFunHandle (type, currentItem, index) {
      if (type === 'filter') { // 筛选列
        if (event.target.checked === false) {
          let addItem = this.tableFilterData.dataNow.filter((item, index, arr) => {
            return currentItem !== item.attr
          })
          this.tableFilterData.dataNow = addItem
        } else {
          let checkData = this.tableFilterData.dataNow2.filter((item, index, arr) => {
            return currentItem === item.attr
          })
          checkData.map((item, index, arr) => {
            this.tableFilterData.dataNow.unshift(item)
          })
        }
      } else if (type === 'allchecked') { // 全选
        if (this.tableFilterData.checkLabels.length === 0) {
          this.tableFilterData.dataNow2.forEach((result) => {
            this.tableFilterData.checkLabels.push(result.attr)
          })
          this.tableFilterData.checkBoxList = this.tableFilterData.checkLabels // 复选框置为复选所有值，全部选择
          this.tableFilterData.dataNow = this.tableFilterData.dataNow2
        } else {
          this.tableFilterData.checkBoxList = this.tableFilterData.checkLabels // 复选框置为复选所有值，全部选择
          this.tableFilterData.dataNow = this.tableFilterData.dataNow2
        }
      } else if (type === 'cancel') { // 取消全选
        this.tableFilterData.checkBoxList = [] // 复选框置为空，全部不选择
        this.tableFilterData.dataNow = []
      }
    },
    getList() {
      this.loading = true;
      listGrant(this.queryParams).then(response => {
        this.grantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        clientId: null,
        clientName: null,
        clientLevel: null,
        getType: null,
        getTime: null,
        grantStaff: null,
        grantRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加营销宣传品发放记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGrant(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改营销宣传品发放记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGrant(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGrant(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");

              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除营销宣传品发放记录编号为"' + ids + '"的数据项？').then(function() {
        return delGrant(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kycinfo/grant/export', {
        ...this.queryParams
      }, `grant_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
